package org.project.single.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.project.single.exception.SystemCodeException;
import org.project.single.mapper.SysUserMapper;
import org.project.single.mapper.SysUserRoleRelMapper;
import org.project.single.pojo.dto.auth.UserInfoAuthDto;
import org.project.single.pojo.po.SysUser;
import org.project.single.pojo.po.SysUserRoleRel;
import org.project.single.pojo.ro.EnableRo;
import org.project.single.pojo.ro.login.BackLoginRo;
import org.project.single.pojo.ro.user.EditBackUserRo;
import org.project.single.pojo.systemEnum.SystemCodeEnum;
import org.project.single.service.SysUserService;
import org.project.single.utils.LoginHelper;
import org.project.single.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stone.project.baseCommon.sysException.BuzException;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleRelMapper sysUserRoleRelMapper;

    @Override
    public Map<String,String> selectUsernameByIds(List<String> ids){
        return sysUserMapper.selectList(Wrappers.lambdaQuery(SysUser.class).in(SysUser::getId,ids))
                .stream()
                .collect(Collectors.toMap(SysUser::getId,SysUser::getUsername));
    }

    @Override
    public String backUserLogin(BackLoginRo backLoginRo) {
        SysUser sysUser = sysUserMapper.selectOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getUsername, backLoginRo.getUsername()));
        if (ObjectUtil.isNotNull(sysUser)){
            if (!sysUser.getPassword().equals(Md5Util.encodeData(backLoginRo.getPassword()))){
                throw new SystemCodeException(SystemCodeEnum.LOGIN_USER_PWD_ERR);
            }
            if (!Integer.valueOf(1).equals(sysUser.getEnableFlag())){
                throw new  SystemCodeException(SystemCodeEnum.LOGIN_USER_PWD_ERR);
            }
            if (Integer.valueOf(1).equals(sysUser.getLockFlag())){
                throw new  SystemCodeException(SystemCodeEnum.LOGIN_USER_PWD_ERR);
            }
            //查询用户授权的所有角色，包括未启用的角色
            List<String> roleCodeList = sysUserRoleRelMapper.selectList(Wrappers.lambdaQuery(SysUserRoleRel.class).eq(SysUserRoleRel::getUserId, sysUser.getId()).orderByAsc(SysUserRoleRel::getUpdateTime))
                    .stream()
                    .map(SysUserRoleRel::getRoleCode)
                    .collect(Collectors.toList());
            UserInfoAuthDto userInfoAuthDto = BeanUtil.copyProperties(sysUser, UserInfoAuthDto.class);
            userInfoAuthDto.setRoleKeyList(roleCodeList);

            //获取token
            return LoginHelper.login(userInfoAuthDto);
        }
        throw new SystemCodeException(SystemCodeEnum.LOGIN_USER_NOT_EXIST);
    }

    @Override
    public void editBackUser(EditBackUserRo editBackUserRo) {
        SysUser sysUser = BeanUtil.copyProperties(editBackUserRo, SysUser.class);
        if (ObjectUtil.isNull(sysUser.getId())){
            sysUser.setId(IdUtil.getSnowflake().nextIdStr());
            sysUser.setLockFlag(0);
            sysUser.setEnableFlag(1);
            sysUser.setDelFlag(0);
            sysUser.setPassword(Md5Util.encodeData(sysUser.getPassword()));
            sysUserMapper.insert(sysUser);
        }else{
            sysUserMapper.updateById(sysUser);
        }
    }

    @Override
    public void enableUser(EnableRo enableRo) {
        SysUser sysUser = BeanUtil.copyProperties(enableRo, SysUser.class);
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public void unLockUser(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setLockFlag(0);
        sysUserMapper.updateById(sysUser);
    }
}
